<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>
<div class="row">
    <div class="col-lg-12">
        <div class="btn-group mb-3" role="group">
            <a role="button" class="btn btn-secondary" href="<?php echo url('collection-list');?>">采集管理</a>
            <a role="button" class="btn btn-secondary active" href="<?php echo url('collection-add');?>">添加采集</a>
        </div>
        <nav>
        <ul class="nav nav-tabs" id="myTab" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">网址规则</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">内容规则</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">高级设置</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="timepost-tab" data-toggle="tab" href="#timepost" role="tab" aria-controls="timepost" aria-selected="false">定时发布</a>
            </li>
        </ul>
        </nav>
        <div class="card">
            <form action="<?php echo url('collection-add');?>" method="post" id="form">
                <div class="card-body">
                    <div class="tab-content" id="myTabContent">
                        <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">采集名称：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="name" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">采集页面编码：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="sourcecharset" value="utf-8" checked> UTF-8</label>
                                    <label><input type="radio" name="sourcecharset" value="gbk"> GBK</label>
                                    <label><input type="radio" name="sourcecharset" value="big5"> BIG5</label>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">采集网址的域名：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="page_base" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">网址类型：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="sourcetype" onclick="showUrlSet('urlpage1');" value="urlpage1" checked> 序列网址</label>
                                    <label><input type="radio" name="sourcetype" onclick="showUrlSet('urlpage2');" value="urlpage2"> 多个网页</label>
                                    <!--<label><input type="radio" name="sourcetype" onclick="showUrlSet('urlpage3');" value="urlpage3"> 单一网页</label>-->
                                </div>
                            </div>
                            <div class="form-group row urltype" id="urlpage1">
                                <label class="col-sm-2 form-control-label">网址配置：</label>
                                <div class="col-sm-10">
                                    <div class="input-group mb-3">
                                        <input type="text" class="form-control" name="urlpage1">
                                        <div class="input-group-append">
                                            <button class="btn btn-outline-secondary" id="testurl" type="button">测试</button>
                                        </div>
                                    </div>
                                    <p>(如：http://bbs.xiuno.com/sky/(*).html,页码使用(*)做为通配符。</p>
                                    <div>
                                        页码从:<input type="text" name="pagesize_start" value="1" size="5" />到
                                        <input type="text" name="pagesize_end" value="10" size="5" />每次增加
                                        <input type="text" name="par_num" value="1" size="5" />
                                    </div>
                                </div>
                            </div>
                            <div class="form-group row urltype" id="urlpage2">
                                <label class="col-sm-2 form-control-label">网址配置：</label>
                                <div class="col-sm-10">
                                    <textarea class="form-control" name="urlpage2"  rows="7"></textarea>
                                </div>
                            </div>
                            <div class="form-group row urltype" id="urlpage3">
                                <label class="col-sm-2 form-control-label">网址配置：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="urlpage3" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">网址配置：</label>
                                <div class="col-sm-10">
                                    网址中必须包含：<input type="text" name="url_contain" />
                                    网址中不得包含：<input type="text" name="url_except" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">获取网址：</label>
                                <div class="col-sm-10">
                                    从：<textarea name="url_start" cols="30" rows="5"></textarea>
                                    到：<textarea name="url_end" cols="30" rows="5"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">标题规则：</label>
                                <div class="col-sm-5">
                                    匹配规则：<textarea class="form-control" name="title_rule" rows="3"><title>[内容]</title></textarea>
                                </div>
                                <div class="col-sm-5">
                                    过滤选项：<textarea class="form-control" name="title_html_rule" rows="3">天空[sky]空天</textarea>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">内容规则：</label>
                                <div class="col-sm-5">
                                    匹配规则：<textarea class="form-control" name="content_rule" rows="3"></textarea>
                                </div>
                                <div class="col-sm-5">
                                    过滤选项：<textarea class="form-control" name="content_html_rule" rows="3"></textarea>
                                </div>
                            </div>
                            <hr />
                            <p style="color: #999;"><strong>注：</strong>内容页分页规则，没有不填</p>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">分页模式：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="content_page_rule" onclick="showUrlSet('content_page_rule1');" value="1" checked> 全部列出模式</label>
                                    <label><input type="radio" name="content_page_rule" onclick="showUrlSet('content_page_rule2');" value="2"> 上下页模式</label>
                                </div>
                            </div>
                            <div class="form-group row content_page_rule" id="content_page_rule1">
                                <label class="col-sm-2 form-control-label">匹配规则：</label>
                                <div class="col-sm-10">
                                    从：<textarea name="content_page_start" cols="30" rows="5"></textarea>
                                    到：<textarea name="content_page_end" cols="30" rows="5"></textarea>
                                </div>
                            </div>
                            <div class="form-group row content_page_rule"  id="content_page_rule2">
                                <label class="col-sm-2 form-control-label">下一页规则：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="content_nextpage" />
                                </div>
                            </div>
                        </div>
                        <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">下载图片：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="down_attachment" value="1" checked> 下载图片</label>
                                    <label><input type="radio" name="down_attachment" value="0"> 不下载</label>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">图片水印：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="watermark" value="0" checked> 不打水印</label>
                                    <label><input type="radio" name="watermark" value="1"> 顶部居左</label>
                                    <label><input type="radio" name="watermark" value="2"> 顶部居中</label>
                                    <label><input type="radio" name="watermark" value="3"> 顶部居右</label>
                                    <label><input type="radio" name="watermark" value="4"> 中部居左</label>
                                    <label><input type="radio" name="watermark" value="5"> 中部居中</label>
                                    <label><input type="radio" name="watermark" value="6"> 中部居右</label>
                                    <label><input type="radio" name="watermark" value="7"> 底部居左</label>
                                    <label><input type="radio" name="watermark" value="8"> 底部居中</label>
                                    <label><input type="radio" name="watermark" value="9"> 底部居右</label>
                                    <label><input type="radio" name="watermark" value="10"> 随机位置</label>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">水印图片路径：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" value="view/img/water-small-xiuno.png" name="watermark_img" placeholder="从网站根目录开始写" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">水印添加条件：</label>
                                <div class="col-sm-10">
                                    宽：<input type="text" size="7" class="" name="watermark_img_w" value="300" />
                                    高：<input type="text" size="7" class="" name="watermark_img_h" value="300" />
                                    <span style="color: #999;">图片宽高大于设定尺寸才会增加水印</span>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">发帖用户ID：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="post_uids" placeholder="多个英文逗号分隔，默认当前登录用户" />
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">发帖时间间隔(分钟)：</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" name="post_times" placeholder="多个英文逗号分隔，默认当前系统时间" />
                                </div>
                            </div>
                        </div>
                        <div class="tab-pane fade" id="timepost" role="tabpanel" aria-labelledby="timepost-tab">
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">定时发布：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="time_post_is" value="0" checked> 关闭</label>
                                    <label><input type="radio" name="time_post_is" value="1"> 开启</label>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">远程计划任务：</label>
                                <div class="col-sm-10">
                                    <label><input type="radio" name="time_post_url" value="0" checked> 关闭</label>
                                    <label><input type="radio" name="time_post_url" value="1"> 开启</label>
                                    <span style="color: #999;">远程计划任务由插件作者的服务器定时触发你网站的发帖api；自己需要使用Linux crontab（推荐）</span>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">发布时间(每天)：</label>
                                <div class="col-sm-10">
                                    <textarea class="form-control" name="time_post_times" rows="5" placeholder="格式：09:00|2 注：每天9点发布2篇，一行一个"></textarea>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">发布版块：</label>
                                <div class="col-sm-10">
                                    <div class="form-check form-check-inline">
                                        <?php if($forums){ foreach($forums as $val) {?>
                                        <label><input type="checkbox" class="form-check-input" value="<?php echo $val['fid'];?>" name="time_post_forumids[]" /><?php echo $val['name'];?></label>
                                        <?php }}?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-2 form-control-label"></label>
                    <div class="col-sm-10">
                        <button type="submit" class="btn btn-primary btn-block" id="submit" data-loading-text="<?php echo lang('submiting');?>..."><?php echo lang('confirm');?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<!-- Modal -->
<div class="modal" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">测试网址：</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <div style="max-height:300px; overflow:auto; ">
                    <ol id="testurllist"></ol>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>

<script>
    var jform = $('#form');
    var jsubmit = $('#submit');
    jform.on('submit', function() {
        jform.reset();
        jsubmit.button('loading');
        var postdata = jform.serialize();
        $.xpost(jform.attr('action'), postdata, function(code, message) {
            if(code == 0) {
                $.alert(message);
                jsubmit.text(message).delay(1000).location(xn.url('collection-list'));
            } else if(xn.is_number(code)) {
                alert(message);
                jsubmit.button('reset');
            } else {
                console.log(code)
                jform.find('[name="'+code+'"]').alert(message).focus();
                jsubmit.button('reset');
            }
        });
        return false;
    });
    function showUrlSet(type) {
        $('.urltype').hide();
        $('.content_page_rule').hide();
        if(type) {
            $('#'+type).show();
        } else {
            $('#urlpage1').show();
            $('#content_page_rule1').show();
        }
    }
    showUrlSet();
    $('#testurl').click(function() {
        $('#exampleModal').modal('show');
        var urlpage1 = $('input[name="urlpage1"]').val();
        var pagesize_start = parseInt($('input[name="pagesize_start"]').val());
        var pagesize_end = parseInt($('input[name="pagesize_end"]').val());
        var par_num = parseInt($('input[name="par_num"]').val());
        var html = '';
        if(urlpage1 && pagesize_start && pagesize_end && par_num) {
            while (pagesize_start <= pagesize_end) {
                html += '<li>' + urlpage1.replace('(*)', pagesize_start) + '</li>';
                pagesize_start = pagesize_start + par_num;
            }
            $('#testurllist').html(html);
        } else {
            $('#testurllist').html('请填写网址参数');
        }
    })

    $('#nav li.nav-item-collection').addClass('active');
</script>